package com.gwclh.personnel.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Department;
import com.gwclh.personnel.entity.DeptTransfer;
import com.gwclh.personnel.entity.Employee;
import com.gwclh.personnel.entity.User;
import com.gwclh.personnel.mapper.DepartmentMapper;
import com.gwclh.personnel.mapper.DeptTransferMapper;
import com.gwclh.personnel.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.utils.ShiroUtil;
import com.gwclh.personnel.vo.transfervo.DeptTransFerVo;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门调动记录表 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
public class DeptTransferServiceImpl extends ServiceImpl<DeptTransferMapper, DeptTransfer> implements IDeptTransferService {
    @Autowired
    IEmployeeService employeeService;
    @Autowired
    IDepartmentService departmentService;
    @Autowired
    IUserService userService;
    @Autowired
    DeptTransferMapper deptTransferMapper;

    @Override
    public ResultVo listTrans(PageDomain pageDomain, DeptTransFerVo deptTransFerVo) {
        Page<DeptTransfer> deptTransferPage = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        deptTransferPage.addOrder(pageDomain.getOrderItemBy());
        QueryWrapper<DeptTransfer> queryWrapper = new QueryWrapper<>();
        if (deptTransFerVo.getEmpId() != null) {
            queryWrapper.eq("emp_id", deptTransFerVo.getEmpId());
        }
        if (!StringUtils.isEmpty(deptTransFerVo.getEmpName())) {
            List<Employee> employees = employeeService.list(new QueryWrapper<Employee>().like("emp_name", deptTransFerVo.getEmpName()));
            List<Integer> ids = new ArrayList<>();
            employees.forEach(employee -> {
                ids.add(employee.getId());
            });
            queryWrapper.in("emp_id", ids);

        }
        if (deptTransFerVo.getTraType() != null) {
            queryWrapper.eq("tra_type", deptTransFerVo.getTraType());
        }
        if (deptTransFerVo.getBeginTime() != null) {
            queryWrapper.ge("create_time", deptTransFerVo.getBeginTime());
        }
        if (deptTransFerVo.getEndTime() != null) {
            queryWrapper.le("create_time", deptTransFerVo.getEndTime());
        }
        this.page(deptTransferPage, queryWrapper);
//        List<DeptTransfer>deptTransfers=deptTransferPage.getRecords();
        List<DeptTransFerVo> deptTransFerVos = new ArrayList<>();

        deptTransferPage.getRecords().forEach(e -> {
            DeptTransFerVo transFerVo = new DeptTransFerVo();
            BeanUtils.copyProperties(e, transFerVo);

            Employee employee = employeeService.getOne(new QueryWrapper<Employee>().eq("id", transFerVo.getEmpId()));
            if (employee != null) {
                transFerVo.setEmpName(employee.getEmpName());
            }
            User user = userService.getOne(new QueryWrapper<User>().eq("id", transFerVo.getOperId()));
            if (user != null) {
                transFerVo.setOperName(user.getUsername());
            }
            Department preDept = departmentService.getOne(new QueryWrapper<Department>().eq("id", transFerVo.getPreDeptId()));
            if (preDept != null) {
                transFerVo.setPreDeptName(preDept.getDeptName());
            }
            Department newDept = departmentService.getOne(new QueryWrapper<Department>().eq("id", transFerVo.getNewDeptId()));
            if (newDept != null) {
                transFerVo.setNewDeptName(newDept.getDeptName());
            }
            deptTransFerVos.add(transFerVo);
        });

        Map<String, Object> result = new HashMap<>();
        result.put("deptTransfers", deptTransFerVos);
        result.put("total", deptTransferPage.getTotal());
        return ResultVo.success(result);
    }

    @Override
    public ResultVo addTrans(DeptTransfer deptTransfer) {
        deptTransfer.setOperId(ShiroUtil.getProfile().getId());
        this.save(deptTransfer);
        employeeService.update(new UpdateWrapper<Employee>().eq("id", deptTransfer.getEmpId()).set("dept_id", deptTransfer.getNewDeptId()));
        return ResultVo.success();
    }

    /**
     * 统计部门调转人数
     * @return
     */
    @Override
    public Map<String, Object> statisticsDeptTransferMonth() {
        return deptTransferMapper.statisticsDeptTransferMonth();
    }

    @Override
    public List<Map<String, Object>> getDeptTransData(Integer id) {
        return deptTransferMapper.selectDeptTransDate(id);
    }
}
